const { __assign } = require('tslib');
const { Liquid, Tokenizer, Hash, Context } = require('liquidjs');
const fs = require('fs');

const quoted = /^'[^']*'|"[^"]*"$/;
exports.stylesheet = {
  parse: function (token, remainTokens) {
    this.processor = token.args;

    this.tokens = [];
    const stream = this.liquid.parser.parseStream(remainTokens);
    stream
      .on('token', (token) => {
        if (token.name === 'endstylesheet') stream.stop();
        else this.tokens.push(token);
      })
      .on('end', () => {
        throw new Error(`tag ${token.raw} not closed`);
      });
    stream.start();
  },
  render: function (ctx, hash) {
    // let processor = "";

    // if (quoted.exec(this.processor)) {
    //     const template = this.processor.slice(1, -1);
    //     processor = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts);
    // }
    // const cont = fs.readFileSync('/src/style/product/index.scss', "utf-8");
    // console.log(cont);
    const text = this.tokens.map((token) => token.getText()).join('');
    // const p = processors[processor]
    // if (!p) throw new Error(`processor for ${processor} not found`)
    // const css = nodeSass.renderSync({
    //     data: text
    // }).css;
    return `<style type="text/css">${text}</style>`;
  },
};

exports.schema = {
  parse: function (tagToken, remainTokens) {
    this.processor = tagToken.args;

    this.tokens = [];
    const stream = this.liquid.parser.parseStream(remainTokens);
    stream
      .on('token', (token) => {
        if (token.name === 'endschema') {
          stream.stop();
        } else {
          this.tokens.push(token);
        }
      })
      .on('end', () => {
        throw new Error(`tag ${tagToken.getText()} not closed`);
      });
    stream.start();
  },
  render: function (ctx, emitter) {
    // if (quoted.exec(this.processor)) {
    //     const template = this.processor.slice(1, -1)
    // }else{
    //     const schema = JSON.parse(this.tokens.map((token) => token.getText()).join(""));
    //     if (!ctx.bottom()["section"] && schema) {
    //         ctx.bottom()["section"] = schema.default;
    //     }
    // }
    return '';
  },
};
